
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2011, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.examples.clustering;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.carrot2.clustering.stc.STCClusteringAlgorithm;
import org.carrot2.core.Cluster;
import org.carrot2.core.Controller;
import org.carrot2.core.ControllerFactory;
import org.carrot2.core.Document;
import org.carrot2.core.IDocumentSource;
import org.carrot2.core.ProcessingResult;
import org.carrot2.examples.ConsoleFormatter;
import org.xmlParser.typeOf;

/**
 * This example shows how to cluster a set of documents available as an {@link ArrayList}.
 * This setting is particularly useful for quick experiments with custom data for which
 * there is no corresponding {@link IDocumentSource} implementation. For production use,
 * it's better to implement a {@link IDocumentSource} for the custom document source, so
 * that e.g., the {@link Controller} can cache its results, if needed.
 * 
 * @see ClusteringDataFromDocumentSources
 * @see UsingCachingController
 */
public class clusterDocumentsStc
{
    //public static void main(String args[]) throws Exception
	
	public ConsoleFormatter abc;

	public StringBuffer clusterDocuments(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent,int  intentLimit, float postClusterCoverage,boolean reMerge, boolean galleries) throws Exception 
    {
		boolean queryType;		//to check if the specified query lies in the whitelist/stop list of some specfic queries
    	StringBuffer ret=new StringBuffer();
            	
            	
            
            //chek the query type..
            typeOf qType =new typeOf();
            String qintent=qType.predictIntent(Q);
            if(qintent!=null){
            
            
            whiteList wh = new whiteList();
            
            HashMap<String, Boolean> list = wh.readWhiteList("stopEntities");
            
            if(list.containsKey(qintent)){
            	System.out.println("Query in the specified list");
            	queryType=false;
            }
            else{
            	System.out.println("Normal Query Execution");
            	queryType=true;
            }
            	
            
            
          
            
            if(queryType){
            	//normal execution
            	//ret=specificQuery( Q,  L,  on,  source, postProcessingCluster, postProcessingCapCount,  intent,  intentLimit, postClusterCoverage, reMerge, galleries,offset,mo,d,mi);
            }
            else{
            	//modified execution
            	
            	//lets think to do something
            }
            }
            else{
            	// not a normal keyword query make a blan guess run normal execution
            	System.out.println("keyord not normal:: no such specified type");
            	//ret=normalQuery( Q,  L,  on,  source, postProcessingCluster, postProcessingCapCount,  intent,  intentLimit, postClusterCoverage, reMerge, galleries,postProcess,offset,mo,d,mi);
            	
            }
            
            return ret;
            

     
            
       }
            
       
	

	
	public StringBuffer normalQuery(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent, int intentLimit,float postClusterCoverage,boolean reMerge,boolean galleries, boolean postProcess)throws Exception
	{
		
	
	    String query=Q;
        int limit=L;
    
		StringBuffer ret=new StringBuffer();
		
	    URLConnectionReader obj = new URLConnectionReader(query,limit,intent);
        
        ArrayList<ArrayList<String>> data1=obj.xmlToDocArray(on, source, intentLimit);
        
        
        
        
        final ArrayList<Document> documents = new ArrayList<Document>();
        
        for (ArrayList<String> row : data1)
        {
            //documents.add(new Document(row[1], row[2], row[0]));
        	documents.add(new Document(row.get(2),row.get(1),row.get(0)));            
        }

        /* A controller to manage the processing pipeline. */
        
        
        final Controller controller = ControllerFactory.createSimple();
        
        final ProcessingResult byTopicClusters = controller.process(documents, Q,STCClusteringAlgorithm.class);
        
        final List<Cluster> clustersByTopic = byTopicClusters.getClusters();
        
        
        
        ret.append("<html><head><script type='text/javascript' src='/lib/jquery-1.7.2.min.js'></script><script type='text/javascript' src='/lib/jquery.mousewheel-3.0.6.pack.js'></script><script type='text/javascript' src='/source/jquery.fancybox.js?v=2.0.6'></script><link rel='stylesheet' type='text/css' href='/source/jquery.fancybox.css?v=2.0.6' media='screen' />	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-buttons.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-buttons.js?v=1.0.2'></script>	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2'></script><script type='text/javascript' src='/source/helpers/jquery.fancybox-media.js?v=1.0.0'></script><script>$(document).ready(function() {$('.fancybox').fancybox({	openEffect	: 'none',	closeEffect	: 'none',nextEffect	: 'none',prevEffect	: 'none'});});</script></head><body>");
        
        ret.append(obj.url);
        
        ret.append("<script>function display(e){var obj=document.getElementById(e);if(obj.style.display=='block'){obj.style.display='none';}else{obj.style.display='block';}}</script>");
        
        abc=new ConsoleFormatter(Q,obj.thumbToUrl);
        
        if(postClusterCoverage>0.0f)abc.setPostClusterCoverage(postClusterCoverage);
        
        abc.setPostProcessingCluster(postProcessingCluster);
        
        abc.setPostProcessingCapCount(postProcessingCapCount);
        
        abc.setReMerge(reMerge);
        
        //setting the galleries flag
        abc.setDispGalleries(galleries);
        
        ret.append(abc.displayClusters(clustersByTopic));
        
        ret.append("</body></html>");
        
        return (ret);
        
        
        

		
		
	}

    }
